package com.surverycomp.models;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.datastore.Key;
import com.surveycomp.servlets.ServletUtils;
import com.surveycomp.utils.HashGenerator;

@PersistenceCapable
public class Event {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private String name;
	@Persistent
	private long datefrom;
	@Persistent
	private long dateTo;
	@Persistent
	private List<Key> challengeKeys;

	@Persistent
	private String secretHash;
	@Persistent
	private String dateToS;
	@Persistent
	private String datefromS;
	@Persistent
	private String email;

	public Event(HttpServletRequest req) throws ParseException {
		this.name = req.getParameter("name");
		setEmail(req.getParameter("email"));
		String dateformat = req.getParameter("dateformat");

		setDatefrom(ServletUtils.getLongDate("datefrom", req, dateformat));
		setDateTo(ServletUtils.getLongDate("dateto", req, dateformat));

		String buffer = name + datefrom + dateTo;
		this.secretHash = HashGenerator.generateHash(buffer);
	}

	public Event(String eventName, long dateFrom, long dateTo) {
		this.name = eventName;
		setDatefrom(dateFrom);
		setDateTo(dateTo);
		String buffer = name + datefrom + dateTo;
		this.secretHash = HashGenerator.generateHash(buffer);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getDatefrom() {
		return datefrom;
	}

	public void setDatefrom(long datefrom) {
		this.datefrom = datefrom;
		this.datefromS = DateFormat.getDateInstance()
				.format(new Date(datefrom));

	}

	public String getDateToS() {
		return dateToS;
	}

	public long getDateTo() {
		return dateTo;
	}

	public void setDateTo(long dateTo) {
		this.dateTo = dateTo;
		this.dateToS = DateFormat.getDateInstance().format(new Date(dateTo));
	}

	public Key getKey() {
		return key;
	}

	public String getSecretHash() {
		return secretHash;
	}

	public List<Key> getChallengeKeys() {
		return challengeKeys;
	}

	public void setChallengeKeys(List<Key> challengeKeys) {
		this.challengeKeys = challengeKeys;
	}

	@Override
	public boolean equals(Object other) {
		if (other == null)
			return false;
		if (other instanceof Event) {

			Event event = (Event) other;
			return name.equals(event.name) && datefrom == event.datefrom
					&& dateTo == event.dateTo
					&& secretHash.equals(event.secretHash);
		}
		return false;
	}

	@Override
	public int hashCode() {
		return (int) (name.hashCode() * datefrom * dateTo * secretHash
				.hashCode());
	}

	public String getDatefromS() {
		return datefromS;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setKey(Key key) {
		this.key = key;
	}
}
